{% load static %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}环境变量管理{% endblock %}</title>

    <!-- Bootstrap 5 CSS -->
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">

    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="{% static 'css/bootstrap-icons.css' %}">

    <style>
        .navbar-brand {
            font-weight: bold;
        }

        .sidebar {
            min-height: calc(100vh - 56px);
            background-color: #f8f9fa;
            border-right: 1px solid #dee2e6;
        }

        .sidebar .nav-link {
            color: #495057;
            padding: 0.5rem 1rem;
        }

        .sidebar .nav-link:hover {
            background-color: #e9ecef;
        }

        .sidebar .nav-link.active {
            background-color: #007bff;
            color: white;
        }

        .main-content {
            padding: 20px;
        }

        .alert {
            margin-bottom: 20px;
        }

        code {
            background-color: #f8f9fa;
            padding: 0.2rem 0.4rem;
            border-radius: 0.2rem;
            font-size: 0.875em;
        }
    </style>

    {% block extra_css %}{% endblock %}
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="{% url 'environment:list' %}">
            <i class="bi bi-server"></i> 系统环境变量管理
        </a>

        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'environment:list' %}">
                        <i class="bi bi-list-ul"></i> 变量列表
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'environment:add' %}">
                        <i class="bi bi-plus-circle"></i> 添加变量
                    </a>
                </li>
            </ul>

            <ul class="navbar-nav">
                {% if user.is_authenticated %}
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                           data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i> {{ user.username }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="/admin/">
                                <i class="bi bi-gear"></i> 管理后台
                            </a></li>
                            <li>
                                <hr class="dropdown-divider">
                            </li>
                            <li><a class="dropdown-item" href="{% url 'admin:logout' %}">
                                <i class="bi bi-box-arrow-right"></i> 退出
                            </a></li>
                        </ul>
                    </li>
                {% else %}
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'admin:login' %}">
                            <i class="bi bi-box-arrow-in-right"></i> 登录
                        </a>
                    </li>
                {% endif %}
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <!-- 侧边栏 -->
        <div class="col-md-3 col-lg-2 sidebar d-none d-md-block">
            <div class="position-sticky pt-3">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link {% if request.resolver_match.url_name == 'list' %}active{% endif %}"
                           href="{% url 'environment:list' %}">
                            <i class="bi bi-list-ul"></i> 所有变量
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link {% if request.resolver_match.url_name == 'add' %}active{% endif %}"
                           href="{% url 'environment:add' %}">
                            <i class="bi bi-plus-circle"></i> 新建变量
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="?refresh=1">
                            <i class="bi bi-arrow-clockwise"></i> 刷新系统变量
                        </a>
                    </li>
                    <li class="nav-item mt-3">
                        <h6 class="px-3 text-muted">系统信息</h6>
                    </li>
                    <li class="nav-item">
                            <span class="nav-link text-muted">
                                <i class="bi bi-cpu"></i> 主机名: {{ request.get_host }}
                            </span>
                    </li>
                    <li class="nav-item">
                            <span class="nav-link text-muted">
                                <i class="bi bi-person"></i> 用户: {{ user.username }}
                            </span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- 主内容区 -->
        <main class="col-md-9 col-lg-10 main-content">
            <!-- 消息提示 -->
            {% if messages %}
                <div class="messages">
                    {% for message in messages %}
                        <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                            {{ message }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}

            <!-- 页面标题 -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2>{% block page_title %}环境变量管理{% endblock %}</h2>
                {% block page_actions %}{% endblock %}
            </div>

            <!-- 主要内容 -->
            {% block content %}
                <div class="alert alert-info">
                    <h4 class="alert-heading"><i class="bi bi-info-circle"></i> 欢迎使用环境变量管理系统</h4>
                    <p>这是一个用于管理Linux系统全局环境变量的Web界面。</p>
                    <hr>
                    <p class="mb-0">请谨慎操作，修改全局环境变量可能会影响系统行为。</p>
                </div>

                <div class="row">
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header">
                                <h5><i class="bi bi-speedometer2"></i> 快速操作</h5>
                            </div>
                            <div class="card-body">
                                <div class="d-grid gap-2">
                                    <a href="{% url 'environment:list' %}" class="btn btn-outline-primary">
                                        <i class="bi bi-list-ul"></i> 查看所有变量
                                    </a>
                                    <a href="{% url 'environment:add' %}" class="btn btn-outline-success">
                                        <i class="bi bi-plus-circle"></i> 添加新变量
                                    </a>
                                    <a href="?refresh=1" class="btn btn-outline-secondary">
                                        <i class="bi bi-arrow-clockwise"></i> 刷新系统变量
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header">
                                <h5><i class="bi bi-exclamation-triangle"></i> 注意事项</h5>
                            </div>
                            <div class="card-body">
                                <ul class="list-group list-group-flush">
                                    <li class="list-group-item">
                                        <small>⚠️ 修改全局变量需要管理员权限</small>
                                    </li>
                                    <li class="list-group-item">
                                        <small>🔒 确保变量名符合命名规范</small>
                                    </li>
                                    <li class="list-group-item">
                                        <small>📁 变量会写入系统配置文件</small>
                                    </li>
                                    <li class="list-group-item">
                                        <small>🔄 某些修改需要重启会话才能生效</small>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            {% endblock %}
        </main>
    </div>
</div>
<style>

    .pagination .page-item.active .page-link {
        background-color: #007bff;
        border-color: #007bff;
    }

    .pagination .page-link {
        color: #007bff;
    }

    .pagination .page-link:hover {
        color: #0056b3;
    }

    .table-hover tbody tr:hover {
        background-color: rgba(0, 123, 255, 0.075);
    }
</style>
<!-- Bootstrap 5 JS -->
<script src="{% static 'js/bootstrap.bundle.min.js' %}"></script>

<!-- 自定义JS -->
<script>
    // 自动隐藏警告消息
    setTimeout(function () {
        var alerts = document.querySelectorAll('.alert');
        alerts.forEach(function (alert) {
            var bsAlert = new bootstrap.Alert(alert);
            bsAlert.close();
        });
    }, 5000);

    // 确认删除对话框
    function confirmDelete(message) {
        return confirm(message || '确定要删除这个环境变量吗？此操作不可撤销。');
    }
</script>

{% block extra_js %}{% endblock %}
</body>
</html>